<template>
  <div class="type-nav">
    <div class="container">
      <div class="nav-left" @mouseleave="updateShow">
        <!-- 鼠标移入移出 显示和隐藏三级分类列表  注意区分组件 -->
        <h2 class="all" @mouseenter="isShow = true">全部商品分类</h2>
        <!-- transition 组件 添加一个过渡动画 -->
        <transition>
          <!-- 动态渲染三级分类列表 -->
          <div class="sort" v-show="isShow">
            <!-- 
              点击跳转到搜索页面：使用事件委派实现
             -->
            <div class="all-sort-list2" @click="goSearch">
              <!-- 
                鼠标移入高亮：设置一个参考值，动态赋值为当前选中的下标
                下标判等，添加类名，鼠标移出时恢复参考值
               -->
              <div
                class="item"
                :class="{ item_on: currentIndex === index }"
                v-for="(one,index) in categoryList"
                :key="one.categoryId"
                @mouseenter="updateMoveIn(index)"
                @mouseleave="currentIndex = -1"
              >
                <h3>
                  <!-- 在标签增加自定义属性，点击跳转搜索页面时，携带当前标签的数据 -->
                  <a
                    :data-categoryname="one.categoryName"
                    :data-category1id="one.categoryId"
                    >{{ one.categoryName }}</a
                  >
                </h3>
                <div class="item-list clearfix">
                  <div class="subitem">
                    <dl
                      class="fore"
                      v-for="tow in one.categoryChild"
                      :key="tow.categoryId"
                    >
                      <dt>
                        <a
                          :data-categoryname="tow.categoryName"
                          :data-category2id="tow.categoryId"
                          >{{ tow.categoryName }}</a
                        >
                      </dt>
                      <dd>
                        <em
                          v-for="there in tow.categoryChild"
                          :key="there.categoryId"
                        >
                          <a
                            :data-categoryname="there.categoryName"
                            :data-category3id="there.categoryId"
                            >{{ there.categoryName }}</a
                          >
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import throttle from 'lodash/throttle'

export default {
  name: "TypeNavComponent",
  data() {
    return {
      isShow: true,
      // 设置一个参考值
      currentIndex: -1,
    };
  },
  mounted() {
    // 当前页面不是home时，隐藏三级分类展示
    if (this.$route.path !== "/home") {
      this.isShow = false;
    }
  },
  methods: {
    // 跳转到搜索页面
    goSearch(e) {
      const { categoryname, category1id, category2id, category3id } =
        e.target.dataset;
      if (categoryname) {
        const option = {
          name: "search",
          // 合并params参数 如果没有params则是一个空对象，不会拼接在URL中
          params:this.$route.params
        };
        option.query = {
          categoryName: categoryname,
          category1Id: category1id,
          category2Id: category2id,
          category3Id: category3id,
        };
        this.$router.push(option);
      }
    },
    // 鼠标移入高亮事件 使用lodash中的函数throttle做节流处理
    updateMoveIn:throttle(function(index){
      this.currentIndex = index
      // trailing:false 在60 毫秒内鼠标移出，最后一次事件不执行 TRUE则执行
    },60,{trailing:false}),
    // 展开和隐藏三级分类列表  根据组件不同 来控制
    updateShow(){
      if(this.$route.path !== '/home'){
        this.isShow = false
      }
    }

  },
  computed: {
    // 映射vuex中的categoryList数据
    ...mapState({ categoryList: (state) => state.home.categoryList }),
  },
};
</script>

<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          &.item_on {
            background-color: skyblue;
          }
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &:hover {
            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}
.v-enter {
  height: 0;
  opacity: 0;
}
.v-enter-active {
  transition: all 1s;
}
.v-enter-to {
  height: 641px;
  opacity: 1;
}
</style>
